//
//  AssignTaskViewController.m
//  leo
//
//  Created by 常艳 on 15/12/14.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "AssignTaskViewController.h"
#import "ChooseTaskViewController.h"
#import "LeoMsg.h"
#import "DataManager.h"
#import "GrowManager.h"
#import "MBFontAdapter.h"
#import "Tooler.h"



@interface AssignTaskViewController ()

@end
static BOOL isChange;
@implementation AssignTaskViewController
@synthesize task;
-(void)dealloc
{
    task=NULL;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.extendedLayoutIncludesOpaqueBars=YES;
    self.title=@"新建任务";
    UIBarButtonItem *rightButton=[[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveTask)];
    [self.navigationItem setRightBarButtonItem:rightButton];
    rightButton=NULL;
    [self.taskView.layer setMasksToBounds:YES];
    self.taskView.layer.cornerRadius=4;
    self.taskView.clipsToBounds=YES;
    self.taskView.layer.borderColor =  [UIColor colorWithRed:0.79 green:0.88 blue:0.92 alpha:1].CGColor;
    self.taskView.layer.borderWidth = 1.0;

    self.topView.layer.borderColor = [UIColor colorWithRed:0.79 green:0.88 blue:0.92 alpha:1].CGColor;
    self.topView.layer.borderWidth = 1.0;
    
    self.bottomView.layer.borderColor = [UIColor colorWithRed:0.79 green:0.88 blue:0.92 alpha:1].CGColor;
    self.bottomView.layer.borderWidth = 1.0;
    GrowDataManager *growDataManager=[[GrowDataManager alloc] init];
    if(growDataManager==NULL)
        return;
    if(task==NULL)
    {
        task=[growDataManager getSysTask:@"1"];
    }

    isChange = NO;

}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];

    [self initUI];
    [self changFont];
    isChange = YES;

}
- (void)initUI
{
    if(task!=NULL&&task.taskId!=NULL)
    {
        if(task.title!=NULL&&![task.title isEqualToString:@"(null)"])
        {
            self.titleLable.text=task.title;
            self.taskTitle.text=task.title;
        }
        if(task.desc!=NULL&&![task.desc isEqualToString:@"(null)"])
        {
            self.taskdesc.text=task.desc;
        }
        if(task.empirical>=0)
        {
            self.taskImg.hidden=NO;
            self.taskemp.text=[NSString stringWithFormat:@"完成后获得%d点经验值!",task.empirical];
        }
        
        if (task.tips != NULL && ![task.title isEqualToString:@"(null)"]) {
                _tipsLabel.text = task.tips;
        }
            _timeLabel.text = [Tooler getCurrentTime];
    }
    
   


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)selectTask:(id)sender {
    ChooseTaskViewController *taskView=[[ChooseTaskViewController alloc] init];
    taskView.assginTaskView=self;
    [self.navigationController pushViewController:taskView animated:YES];
}
-(void)saveTask
{
    if(task==NULL||task.taskId==NULL)
    {
        [LeoMsg showMsg:@"请选择任务"];
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
    {
        [LeoMsg showMsg:@"程序发生错误，code is 5"];
        return;
    }
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
    {
        [LeoMsg showMsg:@"程序发生错误，code is 6"];
        return;
    }
    Watch *selectWatch=[dataManager getActiveWatch:user.uid];
    if(selectWatch==NULL)
    {
        [LeoMsg showMsg:@"没有选中任何手表"];
        return;
    }
    GrowManager *growManager=[[GrowManager alloc] init];
    if(growManager==NULL)
    {
        [LeoMsg showMsg:@"程序发生错误，code is 7"];
        return;
    }
    int taskNums=[dataManager todayAssignTaskNum:selectWatch.deviceUid];
    if(taskNums>=3)
    {
        [LeoMsg showMsg:@"亲，每天只能布置3个任务哦"];
        return;
    }
    task.ownerUid=user.uid;
    task.targetUid=selectWatch.deviceUid;
    task.type=PRESET_TASK;
    task.status=DOING;
    task.action=CONFIRM;
    task.createTime=[Tooler getCurrentTimeStamp];
    task.expireTs=task.expireTs+(long)[task.createTime longLongValue];

    self.navigationItem.rightBarButtonItem.enabled=NO;
    [growManager assignTask:task completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            [LeoMsg showMsg:@"布置任务成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [Tooler showHttpMsg:result description:description msg:@"布置任务失败"];
            self.navigationItem.rightBarButtonItem.enabled=YES;
        }
    }];
}

- (void)changFont
{
    if (isChange == NO) {
        _taskdesc.font =  [MBFontAdapter adjustFont:_taskdesc.font];
        _taskemp.font = [MBFontAdapter adjustFont:_taskemp.font];
        _titleLable.font = [MBFontAdapter adjustFont:_titleLable.font];
        _taskdetail.font = [MBFontAdapter adjustFont:_taskdetail.font];
        _timeLabel.font = [MBFontAdapter adjustFont:_timeLabel.font];
        _tipsLabel.font = [MBFontAdapter adjustFont:_tipsLabel.font];
    }

}

@end
